package jangdolbaeng2.admin.action;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;

import jangdolbaeng2.admin.dao.AdminDao;
import jangdolbaeng2.seller.dao.BuilderDao;
import jangdolbaeng2.seller.dao.ProductManagerDAO;
import jangdolbaeng2.seller.dao.SellerDao;
import jangdolbaeng2.tools.Base64Converter;
import jangdolbaeng2.vo.CategoryVO;
import jangdolbaeng2.vo.ProductVO;
import jangdolbaeng2.vo.SellerVO;
import jangdolbaeng2.vo.ShopVO;
import jangdolbaeng2.vo.Shop_blogVO;
import jangdolbaeng2.vo.ZipcodeVO;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class AdminAction extends ActionSupport {
	
	private SellerVO sellerVO;
	private ShopVO shopVO;
	private ProductVO productVO;
	
	private String seller_id;
	private String password;
	private boolean loginCheck;
	
	private List<ZipcodeVO> zipcodelist;
	private String searchText;
	
	private String shoptxt;
	private String shopSelect;
	private List<ShopVO> shopList;
	private List<ShopVO> shopNameList;
	
	private String sellertxt;
	private String sellerSelect;
	private List<SellerVO> sellerList;
	
	private String categorytxt;
	private char product_category;
	private List<CategoryVO> categoryNameList;
	
	private int product_price;
	private String product_info1;
	private String product_info2;
	
	private int shop_seq;
	private int product_seq;
	private String name;
	private List<ProductVO> productList;
	
	private List<File> uploads = new ArrayList<>();
	private static final String UploadPath = "C:/test/";
	private List<String> uploadsFileName = new ArrayList<>();

	private String photo1;
	private String photo2;
	
	private List<HashMap<String, Object>> shopDayList;
	private List<HashMap<String, Object>> totalDayList;
	private List<HashMap<String, Object>> shopWeekList;
	private List<HashMap<String, Object>> totalWeekList;
	
	private List<HashMap<String, Object>> categoryDayList;
	private List<HashMap<String, Object>> c_totalDayList;
	private List<HashMap<String, Object>> categoryWeekList;
	private List<HashMap<String, Object>> c_totalWeekList;
	
	private List<HashMap<String, Object>> totalVolumeList;
	private List<HashMap<String, Object>> ctotalVolumeList;
	private List<HashMap<String, Object>> shopMonthList;
	/*
	 * 작성자: 류기성
	 */
	public String join()
	{
		String seller_id = sellerVO.getSeller_id();
		shopVO.setSeller_id(seller_id);
		
		new AdminDao().join(sellerVO);
		new AdminDao().shopjoin(shopVO); 
		new AdminDao().blogjoin(new AdminDao().getSeq(sellerVO.getSeller_id()));
		
		return SUCCESS;
	}
	
	public String listZipcode()
	{
		zipcodelist = new AdminDao().listZipcode(searchText);
		return SUCCESS;
	}
	
	public String listZipcode2()
	{
		zipcodelist = new AdminDao().listZipcode(searchText);
		return SUCCESS;
	}
	
	public String shopProduct()
	{
		name = new AdminDao().shopName(productVO.getShop_seq());
		productList = new AdminDao().listProduct(productVO.getShop_seq());
		
		System.out.println(productList);
		
		return SUCCESS;
	}
	
	public String shopProductDetail()
	{
		productVO = new AdminDao().selectProductDetail(productVO.getProduct_seq());
		
		//System.out.println("AdminAction.shopProductDetail(): " + productVO);
		//상품수정시 일반파일이름으로 출력가능?
		if(productVO.getProduct_image1() != null)
		{
			photo1 = "첨부파일1";
		}
		
		if(productVO.getProduct_image2() != null)
		{
			photo2 = "첨부파일2";
		}
		
		return SUCCESS;
	}
	
	public String deleteProduct()
	{
		new AdminDao().deleteProduct(productVO.getProduct_seq());
		
		return SUCCESS;
	}
	
	public String updateProduct()
	{
		AdminDao dao = new AdminDao();
		List<String> list = multiUpload();
		productVO.setProduct_image1(list.get(0));
		
		if(list.size() > 1)
			productVO.setProduct_image2(list.get(1));
		
		dao.updateProduct(productVO);
		
		return SUCCESS;
	}
	
	public String insertProduct()
	{
		AdminDao dao = new AdminDao();
		
		List<String> list = multiUpload();
		productVO.setProduct_image1(list.get(0));
		
		if(list.size() > 1)
		{
			productVO.setProduct_image2(list.get(1));
		}
		
		dao.insertProduct(productVO);
		
		return SUCCESS;
	}
	
	public List<String> multiUpload() {
		List<String> images = new ArrayList<String>();
		Base64Converter b64c = new Base64Converter();			

		for (int index = 0; index < uploads.size(); index++) {
			File destFile = new File(UploadPath + uploadsFileName.get(index));
			try {
				FileUtils.copyFile(uploads.get(index), destFile);
			} catch (IOException e) {
				e.printStackTrace();
			}
			images.add(b64c.imageMaker(destFile.getPath()));
		}
		
		return images;
	}
		
	public String shopDaySales()
	{
		System.out.println("AdminAction.shopDaySales(): " + shop_seq);
		
		shopDayList = new AdminDao().shopDay(shop_seq);
		totalDayList = new AdminDao().totalDay(shop_seq);
		
		System.out.println(totalDayList);
		
		return SUCCESS;
	}
	
	public String shopWeekSales()
	{
		System.out.println("AdminAction.shopWeekSales(): " + shop_seq);
		
		shopWeekList = new AdminDao().shopWeek(shop_seq);
		totalWeekList = new AdminDao().totalWeek(shop_seq);
		shopMonthList = new AdminDao().shopMonth(shop_seq);
		
		System.out.println(shopMonthList);
		
		//System.out.println(totalWeekList);
		
		return SUCCESS;
	}
	
	public String categoryDaySales()
	{
		System.out.println("AdminAction.categoryDaySales(): " + categorytxt);
		
		categoryDayList = new AdminDao().categoryDay(categorytxt);
		c_totalDayList = new AdminDao().ctotalDay(categorytxt);
		
		return SUCCESS;
	}
	
	public String categoryWeekSales()
	{
		System.out.println("AdminAction.categoryWeekSales(): " + categorytxt);
		
		categoryWeekList = new AdminDao().categoryWeek(categorytxt);
		c_totalWeekList = new AdminDao().ctotalWeek(categorytxt);
		
		System.out.println(c_totalWeekList);
		
		return SUCCESS;
	}
	
	public String shopNameList()
	{
		shopNameList = new AdminDao().shopNameList();
		totalVolumeList = new AdminDao().totalVolumeList();
		
		System.out.println(totalVolumeList);
		
		return SUCCESS;
	}
	
	public String categoryNameList()
	{
		categoryNameList = new AdminDao().categoryNameList();
		ctotalVolumeList = new AdminDao().ctotalVolumeList();
		
		System.out.println(ctotalVolumeList);
		
		
		return SUCCESS;
	}
	
	public String sellerDelete()
	{
		new AdminDao().sellerDelete(seller_id);
		
		return SUCCESS;
	}
	
	
	
	
	
	
	
	
	/*
	 * 작성자: 박성곤
	 */
	
	public String login() {
		System.out.println("관리자 아이디 " + seller_id + " 패스워드: " + password);
		sellerVO = new AdminDao().login(seller_id);
		

		if(sellerVO != null && sellerVO.getPassword().equals(password)) {
			ActionContext.getContext().getSession().put("seller_id", seller_id);
			if(!seller_id.equals("admin")) {
				int shopSeq = new AdminDao().getShop_seq(sellerVO.getSeller_id());
				ActionContext.getContext().getSession().put("shop_seq", shopSeq);
				ActionContext.getContext().getSession().put("shop_category",new SellerDao().getShop(shopSeq).getCategory());	// 카테고리를 저장			
			}
			loginCheck = true;
		}
		
		else {
			loginCheck = false;
		}
		return SUCCESS;
	}//login()	
	
	public String shopList(){
		shopList = new AdminDao().shopList(shop_seq);
		
		return SUCCESS;
	}//storeList()
	
	public String sellerList(){
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("sellertxt", sellertxt);	map.put("sellerSelect", sellerSelect);
		
		sellerList = new AdminDao().sellerList(map);

		return SUCCESS;
	}
	
	//Setter, Getter
	
	public SellerVO getSellerVO() {
		return sellerVO;
	}

	public void setSellerVO(SellerVO sellerVO) {
		this.sellerVO = sellerVO;
	}
	
	public String getSeller_id() {
		return seller_id;
	}

	public void setSeller_id(String seller_id) {
		this.seller_id = seller_id;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public boolean isLoginCheck() {
		return loginCheck;
	}
	
	public void setLoginCheck(boolean loginCheck) {
		this.loginCheck = loginCheck;
	}

	public List<ZipcodeVO> getZipcodelist() {
		return zipcodelist;
	}

	public void setZipcodelist(List<ZipcodeVO> zipcodelist) {
		this.zipcodelist = zipcodelist;
	}

	public String getSearchText() {
		return searchText;
	}

	public void setSearchText(String searchText) {
		this.searchText = searchText;
	}

	public String getShoptxt() {
		return shoptxt;
	}

	public void setShoptxt(String shoptxt) {
		this.shoptxt = shoptxt;
	}

	public String getShopSelect() {
		return shopSelect;
	}

	public void setShopSelect(String shopSelect) {
		this.shopSelect = shopSelect;
	}

	public List<ShopVO> getShopList() {
		return shopList;
	}

	public void setShopList(List<ShopVO> shopList) {
		this.shopList = shopList;
	}

	public ShopVO getShopVO() {
		return shopVO;
	}

	public void setShopVO(ShopVO shopVO) {
		this.shopVO = shopVO;
	}

	public String getSellertxt() {
		return sellertxt;
	}

	public void setSellertxt(String sellertxt) {
		this.sellertxt = sellertxt;
	}

	public String getSellerSelect() {
		return sellerSelect;
	}

	public void setSellerSelect(String sellerSelect) {
		this.sellerSelect = sellerSelect;
	}

	public List<SellerVO> getSellerList() {
		return sellerList;
	}

	public void setSellerList(List<SellerVO> sellerList) {
		this.sellerList = sellerList;
	}

	public ProductVO getProductVO() {
		return productVO;
	}

	public void setProductVO(ProductVO productVO) {
		this.productVO = productVO;
	}

	public List<ProductVO> getProductList() {
		return productList;
	}

	public void setProductList(List<ProductVO> productList) {
		this.productList = productList;
	}

	public List<File> getUploads() {
		return uploads;
	}

	public void setUploads(List<File> uploads) {
		this.uploads = uploads;
	}

	public List<String> getUploadsFileName() {
		return uploadsFileName;
	}

	public void setUploadsFileName(List<String> uploadsFileName) {
		this.uploadsFileName = uploadsFileName;
	}

	public static String getUploadpath() {
		return UploadPath;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getProduct_seq() {
		return product_seq;
	}

	public void setProduct_seq(int product_seq) {
		this.product_seq = product_seq;
	}

	public int getShop_seq() {
		return shop_seq;
	}

	public void setShop_seq(int shop_seq) {
		this.shop_seq = shop_seq;
	}

	public int getProduct_price() {
		return product_price;
	}

	public void setProduct_price(int product_price) {
		this.product_price = product_price;
	}

	public String getProduct_info1() {
		return product_info1;
	}

	public void setProduct_info1(String product_info1) {
		this.product_info1 = product_info1;
	}

	public String getProduct_info2() {
		return product_info2;
	}

	public void setProduct_info2(String product_info2) {
		this.product_info2 = product_info2;
	}

	public List<HashMap<String, Object>> getTotalDayList() {
		return totalDayList;
	}

	public void setTotalDayList(List<HashMap<String, Object>> totalDayList) {
		this.totalDayList = totalDayList;
	}

	public List<HashMap<String, Object>> getShopDayList() {
		return shopDayList;
	}

	public void setShopDayList(List<HashMap<String, Object>> shopDayList) {
		this.shopDayList = shopDayList;
	}
	
	public List<HashMap<String, Object>> getShopWeekList() {
		return shopWeekList;
	}

	public void setShopWeekList(List<HashMap<String, Object>> shopWeekList) {
		this.shopWeekList = shopWeekList;
	}

	public List<HashMap<String, Object>> getTotalWeekList() {
		return totalWeekList;
	}

	public void setTotalWeekList(List<HashMap<String, Object>> totalWeekList) {
		this.totalWeekList = totalWeekList;
	}

	public String getCategorytxt() {
		return categorytxt;
	}

	public void setCategorytxt(String categorytxt) {
		this.categorytxt = categorytxt;
	}

	public List<HashMap<String, Object>> getCategoryDayList() {
		return categoryDayList;
	}

	public void setCategoryDayList(List<HashMap<String, Object>> categoryDayList) {
		this.categoryDayList = categoryDayList;
	}

	public List<HashMap<String, Object>> getC_totalDayList() {
		return c_totalDayList;
	}

	public void setC_totalDayList(List<HashMap<String, Object>> c_totalDayList) {
		this.c_totalDayList = c_totalDayList;
	}

	public List<HashMap<String, Object>> getCategoryWeekList() {
		return categoryWeekList;
	}

	public void setCategoryWeekList(List<HashMap<String, Object>> categoryWeekList) {
		this.categoryWeekList = categoryWeekList;
	}

	public List<HashMap<String, Object>> getC_totalWeekList() {
		return c_totalWeekList;
	}

	public void setC_totalWeekList(List<HashMap<String, Object>> c_totalWeekList) {
		this.c_totalWeekList = c_totalWeekList;
	}

	public List<ShopVO> getShopNameList() {
		return shopNameList;
	}

	public void setShopNameList(List<ShopVO> shopNameList) {
		this.shopNameList = shopNameList;
	}

	public char getProduct_category() {
		return product_category;
	}

	public void setProduct_category(char product_category) {
		this.product_category = product_category;
	}

	public String getPhoto1() {
		return photo1;
	}

	public void setPhoto1(String photo1) {
		this.photo1 = photo1;
	}

	public String getPhoto2() {
		return photo2;
	}

	public void setPhoto2(String photo2) {
		this.photo2 = photo2;
	}

	public List<CategoryVO> getCategoryNameList() {
		return categoryNameList;
	}

	public void setCategoryNameList(List<CategoryVO> categoryNameList) {
		this.categoryNameList = categoryNameList;
	}

	public List<HashMap<String, Object>> getTotalVolumeList() {
		return totalVolumeList;
	}

	public void setTotalVolumeList(List<HashMap<String, Object>> totalVolumeList) {
		this.totalVolumeList = totalVolumeList;
	}

	public List<HashMap<String, Object>> getCtotalVolumeList() {
		return ctotalVolumeList;
	}

	public void setCtotalVolumeList(List<HashMap<String, Object>> ctotalVolumeList) {
		this.ctotalVolumeList = ctotalVolumeList;
	}

	public List<HashMap<String, Object>> getShopMonthList() {
		return shopMonthList;
	}

	public void setShopMonthList(List<HashMap<String, Object>> shopMonthList) {
		this.shopMonthList = shopMonthList;
	}
	
	
	
	
}//class
